$(function(){

	var prevMsgId = 0;
	var isOver = 0;
	//聊天内容高度
	$("#article-web").height($(".warp").height()-118);
	$(".article").height($("#main_box").height()-139);
	$("#messageText").width($($("#main_box").width()).width()-14);
	$(".article-close").click(function(){
		$(".article-open").show();
		$(".article-close").hide();
		$(".article-item-ul").hide();
	});
	$(".article-open").click(function(){
		$(".article-open").hide();
		$(".article-close").show();
		$(".article-item-ul").show();
	})

	$(".article-item-ul>li").each(function(i,item){
		$(item).click(function () {
			$(".article").append(html_out(question[i].content)).append(html_in(question[i].answer)).scrollTop(9999);
		})
	})
	//获取输入框内容
	$("#sendBtn").click(function(){
		var msgSubmit = $.trim($(".footer-input,#messageText").val());
		if (msgSubmit.length == 0) {
			alert("发送内容不能为空");
		} else{
			$(".article").append(html_out(msgSubmit)).scrollTop(9999);
			$(".footer-input,#messageText").val("");
			ajaxPostMsg(msgSubmit);
		}
	})

	function ajaxPostMsg (msg) {
		$.post(userData.ajaxUrl , {text:msg,session_id:userData.session_id,referrer:userData.ref,location:userData.href,cid:userData.cid,prevMsgId:prevMsgId,plant:userData.plant,isOver:isOver} , function (data){
			if(200 == data.code && data.msg){
				setTimeout(function () {
					$(".article").append(html_in(data.msg)).scrollTop(9999);
				},2000);
				prevMsgId = data.prevMsgId;
				if(data.status && data.status == 'over'){
					isOver = 1;
				}else{
					isOver = 0;
				}
			}
		},'json');
	}

	
	function html_out (msgSubmit) {
		var str = '<div data-time="" data-id="" id="" data-idserver="" class="article-item-me">';
				userData.plant == 'wap' ? str+= '<img class="article-head-portrait article-pic-right" src="/Public/robot/img/default-icon.png" data-account="wen_admin">' : str+= '';
				str += '<div class="article-item-text article-background-me article-pic-right msg"><div class="article-item-box"><div class="article-item-list">';
				str += msgSubmit;
				str += '</div></div></div></div>';
		return str;
	}

	function html_in (msgSubmit) {
		var str = '<div data-time="" data-id="" id="" data-idserver="" class="article-item-you">'
		userData.plant == 'wap' ? str += '<img class="article-head-portrait article-pic-left" src="http://nim.nos.netease.com/NDIyNjkxMA==/bmltYV8zNzA5MzgxMDNfMTQ5NTEwMTY2NjI2OF9mZjAyYTVlZi04YTYwLTQ3ZWUtYjQyOS0xMjQzMjhmNGU1NmY=?imageView&amp;thumbnail=80x80&amp;quality=85" data-account="wen_admin">' : str += '';
		str += '<div class="article-item-text article-background-you article-pic-left msg-left"><div class="article-item-box"><div class="article-item-list">'
		str +=	msgSubmit   		
		str +='</div></div></div></div>';
		return str;
	}

	// 截取字符串问号后的参数
	function GetQueryString(url , name){
	     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	     var parame = url.indexOf('?');
	     var str = url.substring(parame + 1 , url.length);
	     var r = str.match(reg)
	     if(r!=null)return  unescape(r[2]); return '';
	}

	function keydownSend (msg) {
		$(".article").append(html_out(msg)).scrollTop(9999);
		ajaxPostMsg(msg);
		$(".footer-input").val('');
		$('#messageText').val('').blur().focus();
	}


	$('#messageText').bind('keydown' , function (e) {
		var ev = e || window.event
		var value = $.trim($("#messageText").val());
	    if (value.length > 0) {
	        if (ev.keyCode === 13 && ev.ctrlKey) {
	        	ev.preventDefault();
	        	keydownSend(value);
	        } else if (ev.keyCode === 13 && !ev.ctrlKey) {
	        	ev.preventDefault();
	            keydownSend(value);
	        }
	    }
	})

	$('#header-close').click(function () {
		window.opener=null;
		window.history.back();
		window.close();
	});
})
	
	
	
	
